﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Customer_OrderOnline : System.Web.UI.Page
{
    CustormerMethord cm = new CustormerMethord();
    DataTable tbGioHang = new DataTable();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("Login.aspx");
        }
         
        if (Session["Shopping"] == null)
        {
            Response.Redirect("ShopHollow.aspx");
        }

      
        tbGioHang = (DataTable)Session["Shopping"];
 
 
        string strnumber = tbGioHang.Compute("Sum(Total)", "").ToString();
        lblTotal.Text = strnumber;
        gvOrder.DataSource = tbGioHang;
        gvOrder.DataBind();
        txtDate.Text = DateTime.Now.Date.ToString();
      
    }
   
    
   

protected void btnBuy_Click(object sender, EventArgs e)
{
    int idkh;
            DataTable dr = cm.GetID(Session["UserName"].ToString());
            idkh = Convert.ToInt32(dr.Rows[0][0].ToString());
        if (Session["Shopping"] == null)
        {
            Response.Redirect("ShopHollow.aspx");
        }

        
           
            DateTime date = Convert.ToDateTime(txtDate.Text);
            try
            {
                cm.AddOrder(idkh, date, "In Progress");
            }
            catch (Exception)
            {
                Response.Redirect("Errors.aspx");
            }



            try
            {
                dt = cm.GetOrderID();
            }
            catch (Exception)
            {
                Response.Redirect("Errors.aspx");
            }
            int OrderID = 0;
            foreach (DataRow row in dt.Rows)
            {
                OrderID = int.Parse(row[0].ToString());
            }
            if (tbGioHang.Rows.Count > 0)
            {
                foreach (DataRow row in tbGioHang.Rows)
                {
                    int idSP = int.Parse(row["ProductID"].ToString());
                    int SoLuong = int.Parse(row["Quantity"].ToString());
                    float Gia = float.Parse(row["Price"].ToString());
                    cm.AddOrderDetails(OrderID, idSP, Gia, SoLuong,txtConsigneeName.Text, txtConsigneeAddress.Text,Convert.ToInt32(txtConsigneePhone.Text), txtComment.Text);
                }
            }
            Session["Shopping"] = null;
            Response.Redirect("OrderCompleted.aspx");
        
    }//end buy click


protected void btnCancel_Click(object sender, EventArgs e)
{
    Response.Redirect("Home.aspx");
}
}
